rm(list=ls())
library(foreign)
library(MatchIt)
library(optmatch)
library(Matching)
library(MASS)
library(survival)

#Code to generate Tables 2-13 in the Appendix

#Code for Tables 2-7 load the intervention "snapshot" datasets described in the Appendix, implement the matching routine, and save the resulting matched datasets.

####################################
##### Table 2 - Indirect Rebel #####
####################################

dat.ir<-read.dta("use_indireb.dta")

attach(dat.ir)
mydat.ir <- cbind(indirebel , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.ir.2 <- mydat.ir[complete.cases(mydat.ir),]
detach(dat.ir)

dat.ir.2 <- as.data.frame(dat.ir.2)
nrow(dat.ir.2)

set.seed(031415)
m.out.ir <- matchit(indirebel ~ indigov_dec +  bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + coldwar + polity2 + t0 + num_dyad, method = "nearest", ratio=2, data = dat.ir.2)

m.data.ir <- match.data(m.out.ir)

cbind(round(summary(m.out.ir)$sum.all[,1:2], 3), round(summary(m.out.ir)$sum.matched[,1:2], 3))


#########################################
##### Table 3 - Indirect Government #####
#########################################

dat.ig<-read.dta("use_indigov.dta")

attach(dat.ig)
mydat.ig <- cbind(indigov , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.ig.2 <- mydat.ig[complete.cases(mydat.ig),]
detach(dat.ig)

dat.ig.2 <- as.data.frame(dat.ig.2)
nrow(dat.ig.2)

set.seed(031415)
m.out.ig <- matchit(indigov ~ indirebel_dec +  bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + coldwar + polity2 + t0 + num_dyad, method = "nearest", ratio=2, data = dat.ig.2)

m.data.ig <- match.data(m.out.ig)

cbind(round(summary(m.out.ig)$sum.all[,1:2], 3), round(summary(m.out.ig)$sum.matched[,1:2], 3))


###############################################
##### Table 4 - Direct-Conventional Rebel #####
###############################################

dat.br<-read.dta("use_brutereb.dta")

attach(dat.br)
mydat.br <- cbind(bruterebel , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.br.2 <- mydat.br[complete.cases(mydat.br),]
detach(dat.br)

dat.br.2 <- as.data.frame(dat.br.2)
nrow(dat.br.2)

set.seed(031415)
m.out.br <- matchit(bruterebel ~ indirebel_dec + indigov_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + coldwar + polity2 + t0 + num_dyad, method = "nearest", ratio=2, data = dat.br.2)

m.data.br <- match.data(m.out.br)

cbind(round(summary(m.out.br)$sum.all[,1:2], 3), round(summary(m.out.br)$sum.matched[,1:2], 3))


####################################################
##### Table 5 - Direct-Conventional Government #####
####################################################

dat.bg<-read.dta("use_brutegov.dta")

attach(dat.bg)
mydat.bg <- cbind(brutegov , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.bg.2 <- mydat.bg[complete.cases(mydat.bg),]
detach(dat.bg)

dat.bg.2 <- as.data.frame(dat.bg.2)
nrow(dat.bg.2)

set.seed(031415)
m.out.bg <- matchit(brutegov ~ indirebel_dec + indigov_dec + bruterebel_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + coldwar + polity2 + t0 + num_dyad, method = "optimal", ratio=2, data = dat.bg.2)

m.data.bg <- match.data(m.out.bg)

cbind(round(summary(m.out.bg)$sum.all[,1:2], 3), round(summary(m.out.bg)$sum.matched[,1:2], 3))


#################################################
##### Table 6 - Direct-Unconventional Rebel #####
#################################################

dat.cr<-read.dta("use_coercrebel.dta")

attach(dat.cr)
mydat.cr <- cbind(coercrebel , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.cr.2 <- mydat.cr[complete.cases(mydat.cr),]
detach(dat.cr)

dat.cr.2 <- as.data.frame(dat.cr.2)
nrow(dat.cr.2)

set.seed(031415)
m.out.cr <- matchit(coercrebel ~ indigov_dec + indirebel_dec +  bruterebel_dec + brutegov_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + coldwar  + polity2 + t0 + num_dyad, method = "nearest", ratio=2, data = dat.cr.2)

m.data.cr <- match.data(m.out.cr)

cbind(round(summary(m.out.cr)$sum.all[,1:2], 3), round(summary(m.out.cr)$sum.matched[,1:2], 3))



######################################################
##### Table 7 - Direct-Unconventional Government #####
######################################################

dat.cg<-read.dta("use_coercgov.dta")

attach(dat.cg)
mydat.cg <- cbind(coercgov , indirebel_dec , indigov_dec , bruterebel_dec , brutegov_dec , coercrebel_dec , coercgov_dec, oppose_dec, rel , intens , mtn , eth , coldwar , polity2 , t0, t, gvic, rvic, nset, conflict, num_dyad)

dat.cg.2 <- mydat.cg[complete.cases(mydat.cg),]
detach(dat.cg)

dat.cg.2 <- as.data.frame(dat.cg.2)
nrow(dat.cg.2)

set.seed(031415)
m.out.cg <- matchit(coercgov ~ indigov_dec + indirebel_dec +  bruterebel_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + eth  + polity2 + t0 + num_dyad, method = "nearest", ratio=2, data = dat.cg.2)

m.data.cg <- match.data(m.out.cg)

cbind(round(summary(m.out.cg)$sum.all[,1:2], 3), round(summary(m.out.cg)$sum.matched[,1:2], 3))




#Code for Tables 8-13 estimates competing risks models using the matched data generated above. Results for unmatched data presented in Tables 8-13 comes from the model in the main text (see file: Table 2_Figures1-3.R).


#################################################
##### Table 8 - Direct-Unconventional Rebel #####
#################################################

#Generate time interactions for matched data
m.data.cr$lt <- log(m.data.cr$t)
m.data.cr$coercgov_dec.lt <-m.data.cr$lt*m.data.cr$coercgov_dec
m.data.cr$indirebel_dec.lt <-m.data.cr$lt*m.data.cr$indirebel_dec
m.data.cr$oppose_dec.lt <-m.data.cr$lt*m.data.cr$oppose_dec
m.data.cr$rel.lt <-m.data.cr$lt*m.data.cr$rel
m.data.cr$intens.lt <-m.data.cr$lt*m.data.cr$intens
m.data.cr$eth.lt <-m.data.cr$lt*m.data.cr$eth

#Government Victory
cr.gvic <- coxph(Surv(t0, t, gvic) ~ coercrebel + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indirebel_dec.lt + oppose_dec.lt + intens.lt , data=m.data.cr, method="efron")
summary(cr.gvic)

#Rebel Victory
cr.rvic <- coxph(Surv(t0, t, rvic) ~ coercrebel + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + coercgov_dec.lt + eth.lt + rel.lt, data=m.data.cr, method="efron")
summary(cr.rvic)

#Negotiated Settlement 
cr.nset <- coxph(Surv(t0, t, nset) ~ coercrebel + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indirebel_dec.lt + intens.lt + eth.lt, data=m.data.cr, method="efron")
summary(cr.nset)


######################################################
##### Table 9 - Direct-Unconventional Government #####
######################################################

#Generate time interactions for matched data
m.data.cg$lt <- log(m.data.cg$t)
m.data.cg$oppose_dec.lt <-m.data.cg$lt*m.data.cg$oppose_dec
m.data.cg$rel.lt <-m.data.cg$lt*m.data.cg$rel
m.data.cg$eth.lt <-m.data.cg$lt*m.data.cg$eth

#Government Victory
cg.gvic <- coxph(Surv(t0, t, gvic) ~ coercgov + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + eth + polity2 + oppose_dec.lt + eth.lt + num_dyad + coldwar, data=m.data.cg, method="efron")
summary(cg.gvic)

#Rebel Victory
cg.rvic <- coxph(Surv(t0, t, rvic) ~ coercgov + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + eth + polity2 + rel.lt, data=m.data.cg, method="efron")
summary(cg.rvic)

#Negotiated Settlement
cg.nset <- coxph(Surv(t0, t, nset) ~ coercgov + indirebel_dec + indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + polity2, data=m.data.cg, method="efron")
summary(cg.nset)


################################################
##### Table 10 - Direct-Conventional Rebel #####
################################################

#Generate time interactions for matched data
m.data.br$lt <- log(m.data.br$t + 1)
m.data.br$bruterebel.lt <-m.data.br$lt*m.data.br$bruterebel
m.data.br$coercgov_dec.lt <-m.data.br$lt*m.data.br$coercgov_dec
m.data.br$indirebel_dec.lt <-m.data.br$lt*m.data.br$indirebel_dec
m.data.br$oppose_dec.lt <-m.data.br$lt*m.data.br$oppose_dec
m.data.br$intens.lt <-m.data.br$lt*m.data.br$intens
m.data.br$mtn.lt <-m.data.br$lt*m.data.br$mtn
m.data.br$eth.lt <-m.data.br$lt*m.data.br$eth
m.data.br$polity2.lt <-m.data.br$lt*m.data.br$polity2

#Government Victory
br.gvic <- coxph(Surv(t0, t, gvic) ~ bruterebel + indirebel_dec + indigov_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indirebel_dec.lt + oppose_dec.lt + polity2.lt + coldwar, data=m.data.br, method="efron")
summary(br.gvic)

#Rebel Victory
br.rvic <- coxph(Surv(t0, t, rvic) ~ bruterebel + bruterebel.lt + indirebel_dec + indigov_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + coercgov_dec.lt + intens.lt + num_dyad + coldwar, data=m.data.br, method="efron")
summary(br.rvic)

#Negotiated Settlement
br.nset <- coxph(Surv(t0, t, nset) ~ bruterebel + indirebel_dec + indigov_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + eth + polity2  + intens.lt + mtn.lt + eth.lt + polity2.lt + num_dyad + coldwar, data=m.data.br, method="efron")
summary(br.nset)



#####################################################
##### Table 11 - Direct-Conventional Government #####
#####################################################

#Generate time interactions for matched data
m.data.bg$lt <- log(m.data.bg$t)
m.data.bg$brutegov.lt <-m.data.bg$lt*m.data.bg$brutegov
m.data.bg$bruterebel_dec.lt <-m.data.bg$lt*m.data.bg$bruterebel_dec
m.data.bg$indigov_dec.lt <-m.data.bg$lt*m.data.bg$indigov_dec
m.data.bg$oppose_dec.lt <-m.data.bg$lt*m.data.bg$oppose_dec
m.data.bg$mtn.lt <-m.data.bg$lt*m.data.bg$mtn
m.data.bg$intens.lt <-m.data.bg$lt*m.data.bg$intens
m.data.bg$eth.lt <-m.data.bg$lt*m.data.bg$eth
m.data.bg$polity2.lt <-m.data.bg$lt*m.data.bg$polity2

#Government Victory
bg.gvic <- coxph(Surv(t0, t, gvic) ~ brutegov + brutegov.lt + indirebel_dec + indigov_dec + bruterebel_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indigov_dec.lt + oppose_dec.lt + mtn.lt + polity2.lt, data=m.data.bg, method="efron")
summary(bg.gvic)

#Rebel Victory
bg.rvic <- coxph(Surv(t0, t, rvic) ~ brutegov + indirebel_dec + indigov_dec + bruterebel_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + bruterebel_dec.lt +  intens.lt + eth.lt + polity2.lt + num_dyad + coldwar, data=m.data.bg, method="efron")
summary(bg.rvic)

#Negotiated Settlement
bg.nset <- coxph(Surv(t0, t, nset) ~ brutegov + brutegov.lt + indirebel_dec + indigov_dec + bruterebel_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + oppose_dec.lt + intens.lt + eth.lt + polity2.lt + num_dyad + coldwar, data=m.data.bg, method="efron")
summary(bg.nset)


#####################################
##### Table 12 - Indirect Rebel #####
#####################################

#Generate time interactions for matched data
m.data.ir$lt <- log(m.data.ir$t)
m.data.ir$indirebel.lt <-m.data.ir$lt*m.data.ir$indirebel
m.data.ir$bruterebel_dec.lt <-m.data.ir$lt*m.data.ir$bruterebel_dec
m.data.ir$brutegov_dec.lt <-m.data.ir$lt*m.data.ir$brutegov_dec
m.data.ir$indigov_dec.lt <-m.data.ir$lt*m.data.ir$indigov_dec
m.data.ir$coercrebel_dec.lt <-m.data.ir$lt*m.data.ir$coercrebel_dec
m.data.ir$rel.lt <-m.data.ir$lt*m.data.ir$rel
m.data.ir$oppose_dec.lt <-m.data.ir$lt*m.data.ir$oppose_dec
m.data.ir$intens.lt <-m.data.ir$lt*m.data.ir$intens
m.data.ir$mtn.lt <-m.data.ir$lt*m.data.ir$mtn
m.data.ir$eth.lt <-m.data.ir$lt*m.data.ir$eth
m.data.ir$polity2.lt <-m.data.ir$lt*m.data.ir$polity2

#Government Victory
#coercgov_dec omitted due to non-convergence
ir.gvic <- coxph(Surv(t0, t, gvic) ~ indirebel + indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + oppose_dec + rel + intens + mtn + eth + polity2 + oppose_dec.lt + mtn.lt + eth.lt + num_dyad + coldwar, data=m.data.ir, method="efron")
summary(ir.gvic)

#Rebel Victory
ir.rvic <- coxph(Surv(t0, t, rvic) ~ indirebel + indirebel.lt +  indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 +   brutegov_dec.lt + rel.lt + mtn.lt + eth.lt  + num_dyad + coldwar , data=m.data.ir, method="efron")
summary(ir.rvic)

#Negotiated Settlement
ir.nset <- coxph(Surv(t0, t, nset) ~ indirebel +  indigov_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indigov_dec.lt + bruterebel_dec.lt + coercrebel_dec.lt + oppose_dec.lt + intens.lt + mtn.lt + eth.lt + polity2.lt  + num_dyad + coldwar, data=m.data.ir, method="efron")
summary(ir.nset)



##########################################
##### Table 13 - Indirect Government #####
##########################################

#Generate time interactions for matched data
m.data.ig$lt <- log(m.data.ig$t)
m.data.ig$indigov.lt <-m.data.ig$lt*m.data.ig$indigov
m.data.ig$bruterebel_dec.lt <-m.data.ig$lt*m.data.ig$bruterebel_dec
m.data.ig$brutegov_dec.lt <-m.data.ig$lt*m.data.ig$brutegov_dec
m.data.ig$indirebel_dec.lt <-m.data.ig$lt*m.data.ig$indirebel_dec
m.data.ig$coercgov_dec.lt <-m.data.ig$lt*m.data.ig$coercgov_dec
m.data.ig$oppose_dec.lt <-m.data.ig$lt*m.data.ig$oppose_dec
m.data.ig$rel.lt <-m.data.ig$lt*m.data.ig$rel
m.data.ig$intens.lt <-m.data.ig$lt*m.data.ig$intens
m.data.ig$mtn.lt <-m.data.ig$lt*m.data.ig$mtn
m.data.ig$eth.lt <-m.data.ig$lt*m.data.ig$eth
m.data.ig$polity2.lt <-m.data.ig$lt*m.data.ig$polity2

#Government Victory
ig.gvic <- coxph(Surv(t0, t, gvic) ~ indigov + indigov.lt + indirebel_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + brutegov_dec.lt + oppose_dec.lt + intens.lt + num_dyad + coldwar, data=m.data.ig, method="efron")
summary(ig.gvic)

#Rebel Victory
ig.rvic <- coxph(Surv(t0, t, rvic) ~ indigov + indirebel_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + indirebel_dec.lt + bruterebel_dec.lt + oppose_dec.lt + intens.lt + mtn.lt + polity2.lt + num_dyad + coldwar, data=m.data.ig, method="efron")
summary(ig.rvic)

#Negotiated Settlement
ig.nset <- coxph(Surv(t0, t, nset) ~ indigov + indigov.lt + indirebel_dec + bruterebel_dec + brutegov_dec + coercrebel_dec + coercgov_dec + oppose_dec + rel + intens + mtn + eth + polity2 + brutegov_dec.lt + coercgov_dec.lt + oppose_dec.lt + rel.lt + intens.lt + mtn.lt + eth.lt + polity2.lt + num_dyad + coldwar, data=m.data.ig, method="efron")
summary(ig.nset)
